package org.akaza.openclinica.license;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * Created by jinshaowei on 2017/9/21.
 */
public class ImageLicense {

    /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     * @return
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * @Description: 根据图片地址转换为base64编码字符串
     * @Author:
     * @CreateTime:
     * @return
     */
    public static String getImageStr(String imgFile) {
        byte[] data = getBytes(imgFile);
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /***
     * 从图片中获取签名
     * @param imgFile
     * @param singLength 签名字符长度
     * @return
     */
    public static String getSignStrFromImg(String imgFile, int singLength){
        byte[] data = getBytes(imgFile);
        byte[] signData = new byte[singLength];
        System.arraycopy(data, data.length - singLength, signData, 0, singLength);
        try {
            return new String(signData,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] getBytes(String imgFile) {
        byte[] data = null;
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(final byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }

    /**
     * 示例
     */
    public static void main(String[] args) {
        //String signStr = "1234567890测试";
        String signStr = "bguzImrmqHkrdb0lHYSKN/a17aUmCd8OdaajWtDVyT0uZb455XxPuRhS208srp9yiPNIEkO590lP8zOTfZIadESDPXgqkyaGjRmVqxRERcprjdedjosBH3R7Hs7x/yofN5Bd/I00IBXj/lyUn66EsdCicjOFGOVut4LGiGwrsv6/3PZcA2NOgSqcFcDMinh+YMra9651XTtVMEXN3XkqlHtIkPVfwL0you7mcoKTX6LYQzgjykm3GARuIEI1ZyVBEO3WoVXydFu/13lqFvwixwI59jEP3qG3VOe1GM+2QW6EuWa0B8fT56J1bpF6l1DDTVlyuq8K12nyWKhSgVdANA==";
        String strImg = getImageStr("C:\\Users\\jinshaowei\\Desktop\\123.jpg");
        System.out.println(signStr.getBytes().length);
        strImg = strImg+encode(signStr.getBytes());
        //System.out.println(strImg);
        generateImage(strImg, "C:\\Users\\jinshaowei\\Desktop\\456.jpg");
        System.out.println("===========================");
        System.out.println(getSignStrFromImg("C:\\Users\\jinshaowei\\Desktop\\456.jpg", signStr.getBytes().length));

    }
}
